package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.Clazz;
import com.itheima.entity.ClazzQueryParam;
import com.itheima.entity.Emp;
import com.itheima.entity.PageBean;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    @Autowired
    private EmpMapper empMapper;


    @Override
    public PageBean list(ClazzQueryParam cqp) {
        PageHelper.startPage(cqp.getPage(), cqp.getPageSize());

        List<Clazz> clazzes = clazzMapper.list(cqp);

        Page<Clazz> p = (Page<Clazz>) clazzes;

        List<Clazz> result = p.getResult();

        Set<Integer> masterIds = new HashSet<>();

        result.forEach((clazz)->{
            LocalDate beginDate = clazz.getBeginDate();
            LocalDate endDate = clazz.getEndDate();
            LocalDate nowDate = LocalDate.now();
            if(clazz.getEndDate() != null){
                if(beginDate.isAfter(nowDate)){
                    clazz.setStatus("未开班");
                }
                else if(endDate.isBefore(nowDate)){
                    clazz.setStatus("已结课");
                }
                else{
                    clazz.setStatus("已开班");
                }
            }
            Integer masterId = clazz.getMasterId();
            if(masterId != null){
                masterIds.add(masterId);
            }

        });
        if(!CollectionUtils.isEmpty(masterIds)){
            List<Emp> empList = empMapper.getMasterNameByMasterIds(masterIds);
            Map<Integer, String> empMap = new HashMap<Integer, String>();
            empList.forEach((emp)->{
                empMap.put(emp.getId(), emp.getName());
            });

            result.forEach((clazz)->{
                if(clazz.getMasterId() != null){
                    clazz.setMasterName(empMap.get(clazz.getMasterId()));
                }
            });
        }

        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public PageBean list2(ClazzQueryParam cqp){
        PageHelper.startPage(1, 10);

        List<Clazz> clazzes = clazzMapper.list2(cqp);

        Page<Clazz> p = (Page<Clazz>) clazzes;

        List<Clazz> result = p.getResult();
        result.forEach((clazz)-> {
            LocalDate beginDate = clazz.getBeginDate();
            LocalDate endDate = clazz.getEndDate();
            LocalDate nowDate = LocalDate.now();
            if (clazz.getEndDate() != null) {
                if (beginDate.isAfter(nowDate)) {
                    clazz.setStatus("未开班");
                } else if (endDate.isBefore(nowDate)) {
                    clazz.setStatus("已结课");
                } else {
                    clazz.setStatus("已开班");
                }
            }
        });
        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public void deleteById(Integer id) {

        clazzMapper.deleteById(id);
    }

    @Override
    public void insert(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazz.setCreateTime(LocalDateTime.now());

        LocalDate beginDate = clazz.getBeginDate();
        LocalDate endDate = clazz.getEndDate();
        LocalDate nowDate = LocalDate.now();


        if (clazz.getEndDate() != null) {
            if (beginDate.isAfter(nowDate)) {
                clazz.setStatus("未开班");
            } else if (endDate.isBefore(nowDate)) {
                clazz.setStatus("已结课");
            } else {
                clazz.setStatus("已开班");
            }
        }


        clazzMapper.insert(clazz);
    }

    @Override
    public Clazz findById(Integer id) {

        Clazz clazz = clazzMapper.findById(id);

        return clazz;
    }

    @Override
    public void update(Clazz clazz) {

        clazz.setUpdateTime(LocalDateTime.now());

        clazzMapper.update(clazz);

    }

    @Override
    public List<Clazz> allList() {
        List<Clazz> clazzList = clazzMapper.allList();
        return clazzList;
    }

}
